package com.renge.eduservice.controller.homepage;

import com.renge.commonutils.R;
import com.renge.eduservice.entity.json.ChapterJson;
import com.renge.eduservice.entity.json.FrontCourseInfo;
import com.renge.eduservice.entity.query.FrontCourseVo;
import com.renge.eduservice.service.EduChapterService;
import com.renge.eduservice.service.EduCourseService;
import com.renge.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description="前端首页课程管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/front-course")
public class IndexFrontCourseController {
    @Autowired
    private EduCourseService service;
    @Autowired
    private EduChapterService eduChapterService;
    @ApiOperation(value = "分页查询讲课程列表")
    @PostMapping("/page/{current}/{pagesize}")
    public R findAllByPage(@ApiParam(name = "current",value = "当前页",required = true)
                           @PathVariable("current")Integer current,
                           @ApiParam(name = "pagesize",value = "每页数据量",required = true)
                           @PathVariable("pagesize")Integer pagesize,
                           @ApiParam(name = "FrontCourseVo",value = "条件")
                           @RequestBody(required = false) FrontCourseVo frontCourseVo){

        Map<String,Object>courseMap=service.findFrontAllByPage(current,pagesize,frontCourseVo);
        return R.ok().data("courseMap",courseMap);
    }
    @ApiOperation(value = "根据课程id查询课程的相关信息")
    @PostMapping("/course/{courseid}")
    public R getFrontCourseInformationById(@ApiParam(name = "courseid",value = "课程id",required = true)
                           @PathVariable("courseid")String courseid){
        //查询课程、课程介绍、类别，讲师的相关信息
        FrontCourseInfo courseInfo=service.getFrontCourseInformation(courseid);
        List<ChapterJson> chapterJsons =null;
        //查询的课程可能没有章节
        try {
            //查询章节小节
            chapterJsons = eduChapterService.listChapterVideo(courseid);
        }catch (Exception e){  }
        //封装
        HashMap<String, Object> courseInfoMap = new HashMap<>();
        courseInfoMap.put("courseInfo",courseInfo);
        courseInfoMap.put("chapterJsons",chapterJsons);
        //返回结果
        if(courseInfo==null){
            throw new GuliException(20001,"没有该课程，请确认");
        }
        return R.ok().data("courseInfoMap",courseInfoMap);
    }
}
